package com.zhida.jiyangcenter.account

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.*
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.NetResponse
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_forget_phone.*

@TitleBar(false)
@ContentLayout(R.layout.activity_forget_phone)
class ForgetPhoneActivity:BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        val window = activity.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        title_bar.navigationIcon(R.drawable.iv_back_fill){
            finish()
        }
        iv_phone_clear.setOnClickListener { edit_phone.text = null }
        btn_login.setOnClickListener {
            when {
                edit_phone.text.toString()
                    .isEmpty() -> showSoftInput(edit_phone).apply { tipWarn(R.string.login_phone_empty) }
                !edit_phone.text.toString()
                    .regex(RegexConstants.REGEX_MOBILE_EXACT) -> showSoftInput(edit_phone).apply {
                    tipWarn(
                        R.string.login_phone_wrong
                    )
                }
                else -> {
                    lifecycleScope.request({
                        APIAccount.service().sendCode(edit_phone.text.toString())
                    },
                        object : BaseAPI<Any>() {

                            override fun onError(e: APIException) {
                                tipFail(e.eMessage)
                            }

                            override fun onSuccess(t: Any?, state: Int, msg: String) {
                                startActivity<ForgetCodeActivity> {
                                    putExtra("phone", edit_phone.text.toString())
                                }
                            }
                        })
                }
            }
        }
    }
}